package com.example.carrent.service.Impl;


import com.example.carrent.Util.IDUtils;
import com.example.carrent.Util.RedisUtil;
import com.example.carrent.bean.UserOrders;
import com.example.carrent.mapper.UserOrdersMapper;
import com.example.carrent.service.UserOrdersService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
//@Transactional
public class UserOrdersServiceImpl implements UserOrdersService {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    private UserOrdersMapper userOrdersMapper;

    @Autowired
    private RedisUtil redisUtil;


    /** 支付订单*/
//    必传 订单 id userid  已完成状态  修改时间
    @Override
    public String payOrder(UserOrders userOrders) {
       String lockKey = "course:purchase:" + userOrders.getUserId() + ":" + userOrders.getCourseId();
       String lockValue = IDUtils.randomId();
       if(redisUtil.delete(lockKey)){
           // 课程价格
           //Double price = userOrders.getPaymentAmount();
           //Users users = userOrdersMapper.findUserById(userOrders.getUserId());
           //if ( users.getMoney()-price >=0){
               //users.setMoney(users.getMoney()-price);


               userOrdersMapper.updateById(userOrders);
               //插入用户和课程信息 此表用来记录用户购买的课程
               userOrdersMapper.insertUserAndCourses(userOrders);
           //}else {
               //redisUtil.setIfAbsent(lockKey, lockValue, 15, TimeUnit.MINUTES);
              // return "余额不足，请充值！";
          // }
       }else {
           return "您已经下单过该课程，请勿重复购买！";
       }
       return "支付成功！";

    }



}
